﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.Hrs.Shared.DataContracts.DataObjects;
using Xyz.Hrs.Shared.DataContracts.Criterias;
using Xyz.Framework.Server.DataAcessLayer;
using Xyz.Hrs.Shared.Utils;
using System.Data.Objects;
using Xyz.Hrs.Server.DomainObjects;

namespace Xyz.Hrs.Server.HrsManagementBL
{
    public class HospitalManagement
    {
        public const string CONNECTION_STRING = "name = HRSDomainEntities";
        public const string CONTAINER_NAME = "HRSDomainEntities";
        public static HospitalDetailsDTO[] SearchHospitals(SearchHospitalCriteria criteria)
        {
            using (DataBaseContext db = new DataBaseContext(CONNECTION_STRING, CONTAINER_NAME))
            {
                ObjectSet<Hospital> _hospitalSet = db.CreateObjectSet<Hospital>();
                ObjectSet<HostpitalPolyclinic> _hospitalPolyclinicSet = db.CreateObjectSet<HostpitalPolyclinic>();
                ObjectSet<Doctor> _doctorSet = db.CreateObjectSet<Doctor>();
                ObjectSet<Polyclinic> _polyclinicSet = db.CreateObjectSet<Polyclinic>();

                bool townisEmpty = criteria.TownIds == null || criteria.TownIds.Length == 0;
                bool hospitalisEmpty = criteria.HospitalIds == null || criteria.HospitalIds.Length == 0;
                bool polyclinicisEmpty = criteria.PolyclinicIds == null || criteria.PolyclinicIds.Length == 0;

                var _query = from h in _hospitalSet
                             where
                             (townisEmpty || criteria.TownIds.Contains(h.TownId))
                             join hp2 in
                                 (
                                     from hp in _hospitalPolyclinicSet
                                     where
                                     (polyclinicisEmpty || criteria.PolyclinicIds.Contains(hp.PolyclinicId))
                                     join d in _doctorSet on hp.Id equals d.HospitalPolyclinicId into doctors
                                     join p in _polyclinicSet on hp.PolyclinicId equals p.Id
                                     select new
                                     {
                                         hospitalpolyclinicId = hp.Id,
                                         hp.HospitalId,
                                         hp.OpeningDate,
                                         hp.State,
                                         hp.TotalDailyPatient,
                                         p.Id,
                                         p.Name,
                                         p.Description,
                                         doctors
                                     }
                                 ) on h.Id equals hp2.HospitalId into polyclinics
                             select new
                             {
                                 hospital = h,
                                 polyclinics
                             };
                _query = _query.Skip(criteria.index).Take(criteria.count);
                var _queryresult = _query.ToArray();

            }
            return null;
        }
    }
}
